package com.zyq.controller;

/**
 * 用户首页Controller
 * @author: zyq
 * @create: 2022-03-03
 **/

import com.zyq.mapper.SearchWordMapper;
import com.zyq.model.common.ResponseBean;
import com.zyq.model.response.HomePageResponse;
import com.zyq.service.IArticleService;
import com.zyq.service.IMessageService;
import com.zyq.service.ITopicService;
import com.zyq.service.IUserService;
import com.zyq.util.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Api( tags = "用户首页")
@RestController
@RequestMapping("/homePage")
public class HomePageController {

    @Autowired
    private ITopicService topicService;

    @Autowired
    private IArticleService articleService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IMessageService messageService;

    @Autowired
    private UserUtil userUtil;

    @Autowired
    private SearchWordMapper searchWordMapper;
    /**
     * 糖尿病友之家首页
     */
    @ApiOperation("糖尿病友之家首页")
    @GetMapping
    public ResponseBean adminList(){
        HomePageResponse homePageResponse = new HomePageResponse();
        homePageResponse.setTopic10(topicService.select10())
                .setArticle10(articleService.select10())
                .setUser10(userService.select10())
                .setWords(searchWordMapper.selectWord())
                .setArticle2(articleService.select2());
        Subject subject = SecurityUtils.getSubject();
        // 登录了返回true
        if (subject.isAuthenticated()){
            Long userId = userUtil.getUserId();
            homePageResponse.setCountMessage(messageService.countByUserId(userId));
        }
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", homePageResponse);
    }
}
